/**
 * 
 */
package com.hk.Mgr.web.controller;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hk.Mgr.web.message.StatusCode;
import com.hk.Mgr.web.message.department.DepartmentReq;
import com.hk.Mgr.web.message.department.DepartmentRes;
import com.hk.base.BaseCtrl;
import com.hk.base.ResponseBase;
import com.hk.commons.HqlFilter;
import com.hk.commons.ReqToModelHelper;
import com.hk.commons.entity.Department;
import com.hk.service.sys.IDepartment;

/**
 * @author psx 部门实现类
 */
@Controller
@RequestMapping(value = BaseCtrl.MgrRootPath + "/DepartmentCtrl")
public class DepartmentCtrl extends BaseCtrl {

	@Autowired
	private IDepartment departmentService;

	/**
	 * @Title: getDepartmentById
	 * @Description: 根据id获取系统模块信息
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getDepartmentById", method = RequestMethod.POST)
	public ResponseBase<DepartmentRes> getDepartmentById(
			@RequestBody DepartmentReq req) {
		ResponseBase<DepartmentRes> resp = new ResponseBase<DepartmentRes>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}
			Department department = departmentService.findById(req.getId());

			DepartmentRes singleResp = new DepartmentRes();
			ReqToModelHelper.copyReqValueToModel(department, singleResp);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setResult(singleResp);
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: getDepartmentList
	 * @Description: 分页获取系统模块
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/getDepartmentList", method = RequestMethod.POST)
	public ResponseBase<List<DepartmentRes>> getDepartmentList(
			@RequestBody DepartmentReq req) {
		ResponseBase<List<DepartmentRes>> resp = new ResponseBase<List<DepartmentRes>>();
		HqlFilter hqlFilter = new HqlFilter();
		if (StringUtils.isNotEmpty(req.getName())) {
			hqlFilter.addLKFilter("name", req.getName());
		}
		hqlFilter.addSort("id");
		hqlFilter.addOrder("desc");
		
		try {
			List<Department> departmentList = departmentService.findByFilter(
					hqlFilter, req.getCurPage(), req.getPageSize());

			List<DepartmentRes> respList = new ArrayList<DepartmentRes>();
			// 根据模块id查询资源
			if (departmentList != null && !departmentList.isEmpty()) {
				DepartmentRes singleResp = null;
				for (Department department : departmentList) {
					singleResp = new DepartmentRes();
					 
					ReqToModelHelper
							.copyReqValueToModel(department, singleResp);
					respList.add(singleResp);
				}
			}
			Long total = departmentService.countByFilter(hqlFilter);
			resp.setSize(total);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setResult(respList);
			
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: saveDepartment
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/saveDepartment", method = RequestMethod.POST)
	public ResponseBase<Department> saveDepartment(
			@RequestBody DepartmentReq req) {
		ResponseBase<Department> resp = new ResponseBase<Department>();
		try {
			Department model = new Department();
			model.setCreatedts( this.getCurrentTime());
			model.setCreator(this.getAccount());
			model.setName(req.getName());
			departmentService.add(model);

			resp.setResult(model);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("添加成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: updateDepartment
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/updateDepartment", method = RequestMethod.POST)
	public ResponseBase<Department> updateDepartment(@RequestBody DepartmentReq req) {
		ResponseBase<Department> resp = new ResponseBase<Department>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}
			Department model = departmentService.findById(req.getId());
			model.setName(req.getName());
			departmentService.update(model);

			resp.setResult(model);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("修改成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}

	/**
	 * @Title: deleteDepartment
	 * @Description: TODO
	 * @param
	 */
	@ResponseBody
	@RequestMapping(value = "/deleteDepartment", method = RequestMethod.POST)
	public ResponseBase<Department> deleteDepartment(@RequestBody DepartmentReq req) {
		ResponseBase<Department> resp = new ResponseBase<Department>();
		try {
			if (req == null || req.getId() == null) {
				resp.setResultCode(StatusCode.PARAM_ERROR.key());
				resp.setMsg("参数不对：id is null！");
				return resp;
			}

			Department ret = departmentService.delete(req.getId());

			resp.setResult(ret);
			resp.setResultCode(StatusCode.SUCCESS.key());
			resp.setMsg("删除成功");
		} catch (Exception ex) {
			logger.error(ex.getMessage(), ex);
			resp.setMsg("操作异常，请重试");
			resp.setResultCode(StatusCode.EXCEPTION.key());
		}
		return resp;
	}
}
